package com.homework;

import java.util.*;

/**
 * @author 于振晗
 * @title: homework01
 * @description: TODO 使用List和Map存放多个图书信息，遍历并输出。
 *                      其中商品属性：编号，名称，单价，出版社；使用商品编号作为Map中的key
 * @date 2020年09月03日
 * @path com.homework
 */
public class homework01 {
    public static void main(String[] args) {
        Book book1 = new Book(1, "十万个为什么1", 155.5, "1清华出版社");
        Book book2 = new Book(2, "十万个为什么2", 255.5, "2清华出版社");
        Book book3 = new Book(3, "十万个为什么3", 355.5, "3清华出版社");
        Book book4 = new Book(4, "十万个为什么4", 455.5, "4清华出版社");
        Book book5 = new Book(5, "十万个为什么5", 555.5, "5清华出版社");
        Book book6 = new Book(6, "十万个为什么6", 655.5, "6清华出版社");

        List<Book> lists = new ArrayList();
        lists.add(book1);
        lists.add(book2);
        lists.add(book3);
        lists.add(book4);
        lists.add(book5);
        lists.add(book6);

        Map<Integer,Book> mp = new HashMap();
        mp.put(book1.getId(),book1);
        mp.put(book2.getId(),book2);
        mp.put(book3.getId(),book3);
        mp.put(book4.getId(),book4);
        mp.put(book5.getId(),book5);
        mp.put(book6.getId(),book6);
        lists.forEach(item-> System.out.println(item));



        Set<Map.Entry<Integer, Book>> entries = mp.entrySet();
        entries.forEach(item-> System.out.println(item.getKey()+"--"+item.getValue()));
    }
}


class Book implements Comparable<Book>{
    private int id;
    private String name;
    private double price;
    private String publisher;

    public Book(int id, String name, double price, String publisher) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.publisher = publisher;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", publisher='" + publisher + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (id != book.id) return false;
        if (Double.compare(book.price, price) != 0) return false;
        if (name != null ? !name.equals(book.name) : book.name != null) return false;
        return publisher != null ? publisher.equals(book.publisher) : book.publisher == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (publisher != null ? publisher.hashCode() : 0);
        return result;
    }

    @Override
    public int compareTo(Book o) {
        if(name.compareTo(o.getName()) == 0){
            return name.compareTo(o.getName());
        }
        return id - o.id;
    }
}